// pages/cpxq/cpxq.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图
    imgUrls: [
      '/images/dj1.png',
      '/images/dj1.png',
      '/images/dj1.png'
    ],
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,  //是否自动切换
    interval: 4000,  //自动切换时间间隔
    circular: true,  //是否采用衔接滑动
    //end
    //页面数据
    data:[],
    //用户信息
    user:'',
    //收藏
    collection: false,


    //购买弹框
    showModalStatus: false,//是否显示
    gg_id: 0,//规格ID
    gg_txt: '',//规格文本
    gg_price: 0,//规格价格
    guigeList: [{ guige: '100', price: '150' }, { guige: '500', price: '350' }, { guige: '800', price: '650' }],
    num: 1,//初始数量
    minusStatus: 'disabled',
    spec_id:"",//规格id
    spec_price:{},
    goods_spec_price_key:"",


    //倒计时
    endTime: '2020-11-22 10:40:30',//2020/11/22 10:40:30这种格式也行

    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options)
    that.countDown()
    app.wxRequest('POST','/Home/Goods/goods_list',{'goods_id':options.goods_id,'shop_id':options.shop_id},function(res){
      console.log(res)
      if(res.data.code == 1){
        that.setData({
          data:res.data.data,
          guigeList:res.data.spec,
          imgUrls:res.data.data.thumb,
          gg_price:res.data.data.new_price
        })
        console.log(res.data.data.thumb)
      }
    },function(err){

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo){
      console.log(userInfo);
      this.setData({
        user: userInfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "好商品-就在同城找！",
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }

  },


  /*收藏*/
  collection: function () {
    let that = this.data;
    var collection = that.collection
    var col = collection ? false : true;
    this.setData({
      collection: col
    })
  },

  ////购买弹框
  filter: function (e) {
    console.log(e.currentTarget.dataset);
    var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, price = e.currentTarget.dataset.price
    self.setData({
      gg_id: id,
      gg_txt: txt,
      gg_price: price,
      spec_id:e.currentTarget.dataset.spec_id
    });
    var id = {};
    var p_spec_name = e.currentTarget.dataset.p_spec_name;

    let spec_price = this.data.spec_price;
    let item = e.currentTarget.dataset.p_text;
    spec_price[item] = e.currentTarget.dataset.id;
   console.log(self.data.spec_price)
    self.setData({
      spec_price
    });
 


   var spec_price_all = JSON.stringify(self.data.spec_price);
   console.log(spec_price_all)
    app.wxRequest('POST','/Home/Goods/goods_spec_price',{
      'spec_price':spec_price_all,
      },
      function(res){
      console.log(res)
      if(res.data.code == 1){
        console.log(res.data.data.price)
        self.setData({
          gg_price:res.data.data.price,
          goods_spec_price_key:res.data.data.key        
        })
       //console.log(res.data.data)
      }
    },function(err){
    })
  },

  addCart(res){
      var that = this;
      var spec_price = that.data.goods_spec_price_key;//规格
      var goods_id = that.data.data.id;//商品id
      var shop_id = that.data.data.shop_id;//商家
      var num = that.data.num;//数量
      var uid = that.data.user.id;
   

      if(spec_price == ''){
        wx.showToast({
          title: '请选择规格',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      app.wxRequest('POST','/Home/Goods/add_shopping_cart',{
        'spec_price':spec_price,
        'goods_id':goods_id,
        'shop_id':shop_id,
        'num':num,
        'uid':uid
        },
        function(res){
        console.log(res)
        if(res.data.code == 1){
      
            wx.showToast({
              title: '添加购物车成功',
              icon: 'success',
              duration: 2000
            })
         //console.log(res.data.data)
        }
      },function(err){
      })
     
      
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  //显示对话框
  showModal: function () {
    //判断用户是否已经登录
    var user = this.data.user;
    if(user == ''){
      wx.showModal({
        title: '提示',
        content: '请先登录账号',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/user/user'
            });
          } else if (res.cancel) {
            wx.navigateBack({//返回
              delta: 1
            })
          }
        }
      })
    }
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
//end

  // 倒计时
  countDown: function () {
    var that = this;
    var nowTime = new Date().getTime();//现在时间（时间戳）
    var endTime = new Date(that.data.endTime).getTime();//结束时间（时间戳）
    var time = (endTime - nowTime) / 1000;//距离结束的毫秒数
    // 获取天、时、分、秒
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    // console.log(day + "," + hou + "," + min + "," + sec)
    day = that.timeFormin(day),
      hou = that.timeFormin(hou),
      min = that.timeFormin(min),
      sec = that.timeFormin(sec)
    that.setData({
      day: that.timeFormat(day),
      hou: that.timeFormat(hou),
      min: that.timeFormat(min),
      sec: that.timeFormat(sec)
    })
    // 每1000ms刷新一次
    if (time > 0) {
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    } else {
      that.setData({
        countDown: false
      })
    }
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0 : param;
  },



//领劵
  /**
   * 点击拉起栏目
   */

  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    console.log(currentStatu);
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(240).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })

      //关闭抽屉
      if (currentStatu == "close1") {
        this.setData(
          {
            showModalStatus1: false
          }
        );
      }

    }.bind(this), 200)

    // 显示抽屉
   
    if (currentStatu == "open1") {
      this.setData(
        {
          showModalStatus1: true
        }
      );
    }
    

  },





})